<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template #after>
          <el-button type="primary" size="small" @click="addPermission(1,0)">添加权限</el-button>
        </template>
      </page-tools>

      <!-- 表格 -->
      <!-- 指定id为唯一属性标识 -->
      <el-table border="" :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column align="center" prop="code" label="权限标识" />
        <el-table-column align="center" prop="description" label="描述" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{row}">
            <!-- 添加按钮只在访问权的层级显示 -->
            <el-button v-if="row.type ===1 ? true:false" type="text" @click="addPermission(2,row.id)">添加</el-button>
            <el-button type="text" @click="updatePermission(row.id)">查看</el-button>
            <el-button type="text" @click="removeCancel(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 新增和编辑弹层 -->
      <el-dialog :title="formData.id ? '编辑权限':'新增权限'" :visible="showDialog" @close="btnCancel">
        <el-form ref="formData" :model="formData" :rules="rules" label-width="120px">
          <el-form-item label="权限名称" prop="name">
            <el-input v-model="formData.name" style="width:85%" />
          </el-form-item>
          <el-form-item label="权限标识" prop="code">
            <el-input v-model="formData.code" style="width:85%" />
          </el-form-item>
          <el-form-item label="权限描述" prop="description">
            <el-input v-model="formData.description" style="width:85%" />
          </el-form-item>
          <el-form-item label="企业可见">
            <el-switch
              v-model="formData.enVisible"
              active-text="可见"
              inactive-text="不可见"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-row type="flex" justify="center">
            <el-button @click="btnCancel">取消</el-button>
            <el-button type="primary" @click="btnOK">确定</el-button>
          </el-row>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { getPermissionList, delPermission, addPermisson, getPermissionDetail, updatePermission } from '@/api/permisson'
import { tranListToTreeDate } from '@/utils'
export default {
  data() {
    return {
      list: [],
      showDialog: false,
      formData: {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型
        pid: '', // 树，需要知道哪个节点下的
        enVisible: '0' // 开启
      },
      rules: {
        name: [
          { required: true, message: '权限不能为空', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '权限标识不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getPermissionList()
  },
  methods: {
    async getPermissionList() {
      // 将数据转化成带children的树形结构
      this.list = tranListToTreeDate(await getPermissionList(), '0')
    },
    addPermission(type, pid) {
      // type表示类型，pid表示当前数据的标识
      // 记录类型和标识
      this.formData.type = type
      this.formData.pid = pid
      this.showDialog = true
    },
    btnCancel() {
      this.showDialog = false
      this.$refs.formData.resetFields()
      this.formData = {
        name: '', // 名称
        code: '', // 标识
        description: '', // 描述
        type: '', // 类型
        pid: '', // 树，需要知道哪个节点下的
        enVisible: '0' // 开启
      }
    },
    removeCancel(id) {
      this.$confirm('您确定删除此权限吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delPermission(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getPermissionList()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    async btnOK() {
      try {
        await this.$refs.formData.validate()
        try {
          if (this.formData.id) {
            await updatePermission(this.formData)
            this.$message.success('更新成功')
          } else {
            await addPermisson(this.formData)
            this.$message.success('添加成功！')
          }
          this.getPermissionList()
          this.showDialog = false
        } catch (err) {
          this.$message.error('添加失败，请稍后重试')
        }
      } catch (err) {
        console.log(err)
      }
    },
    async updatePermission(id) {
      this.formData = await getPermissionDetail(id)
      this.showDialog = true
    }
  }
}
</script>

<style>

</style>
